package com.zmcn.flowindex.business.title.dao;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.hibernate.Query;
import org.springframework.stereotype.Component;

import com.zmcn.flowindex.bean.po.IndexInfo;
import com.zmcn.flowindex.bean.po.IndexTitle;
import com.zmcn.flowindex.bean.po.UploadBatch;
import com.zmcn.flowindex.bean.vo.IndexTitleVO;
import com.zmcn.flowindex.business.title.vo.IndexInfoVO;
import com.zmcn.flowindex.business.title.vo.ShowVO;
import com.zmcn.flowindex.fw.action.Page;
import com.zmcn.flowindex.fw.dao.BaseDao;

/**
 * 
 * 指标主题数据处理层
 * 
 * @author <b>欧建东<br/>
 *         Email:oyjd@163.com</b>
 * @version 1.0
 */
@Component
public class titleDao extends BaseDao {
	/**
	 * 获取指标主题列表数据
	 * 
	 * @param page
	 *            页面对象
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public Page<ShowVO> getList(Page<ShowVO> page) {
		String hqlResult = "select new " + ShowVO.class.getName() + ShowVO.STRUCT;
		String hqlCount = "select count(indextitle)";
		StringBuffer sb = new StringBuffer();
		sb.append(" from ").append(IndexTitle.class.getName()).append(" indextitle,");
		sb.append(UploadBatch.class.getName()).append(" uploadbatch");
		sb.append(" where uploadbatch.status=0 and uploadbatch.batchId=indextitle.batchId");
		Map<String, String[]> paras = page.getParas();
		List<Object> ps = new ArrayList<Object>();
		for (String key : paras.keySet()) {
			if ("titleName".equals(key)) {
				sb.append(" and indextitle.").append(key).append(" like ?");
				ps.add("%" + paras.get(key)[0] + "%");
			} else if ("uploadTime".equals(key)) {// 开始日期 时间 格式：2012-09-08
				sb.append(" and DATE_FORMAT(uploadbatch.lastUpdateTime,'%Y-%m-%d')=?");
				ps.add(paras.get(key)[0]);
			}
		}
		sb.append(" order by uploadbatch.lastUpdateTime desc");
		// do count
		Query qCount = getSession().createQuery(hqlCount + sb.toString());
		for (int i = 0; i < ps.size(); i++) {
			qCount.setParameter(i, ps.get(i));
		}
		Long count = (Long) qCount.uniqueResult();
		page.setCount(count);
		// do result
		Query q = getSession().createQuery(hqlResult + sb.toString());
		for (int i = 0; i < ps.size(); i++) {
			q.setParameter(i, ps.get(i));
		}
		q.setFirstResult(page.getFirst());
		q.setMaxResults(page.getPageSize());
		page.setResult(q.list());
		return page;
	}

	/**
	 * 获取某主题的节点树数据
	 * 
	 * @param titleId
	 *            主题ID
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<IndexInfoVO> getTreeByTitle(String titleId) {
		StringBuffer sb = new StringBuffer();
		sb.append("select new ").append(IndexInfoVO.class.getName()).append(IndexInfoVO.STRUCT);
		sb.append(" from ").append(IndexInfo.class.getName()).append(" indexinfo");
		sb.append(" where indexinfo.titleId=?");
		Query q = this.getSession().createQuery(sb.toString());
		q.setParameter(0, titleId);
		List<IndexInfoVO> tmpList = (List<IndexInfoVO>) q.list();
		return tmpList;
	}

	/**
	 * 删除指标主题
	 * 
	 * @param uploadTime
	 *            上传时间
	 * @param user
	 *            当前用户
	 * @return
	 */
	public void remove(String uploadTime, String user) {
		StringBuffer sb = new StringBuffer();
		sb.append("update ").append(UploadBatch.class.getName()).append(" set status=1, lastEditor=?, lastUpdateTime=? where DATE_FORMAT(lastUpdateTime,'%Y-%m-%d')=?");
		Query q = getSession().createQuery(sb.toString());
		q.setParameter(0, user);
		q.setParameter(1, new Date());
		q.setParameter(2, uploadTime);
		q.executeUpdate();
	}

	/**
	 * 获取最后上传指标的标题列表
	 * 
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<IndexTitleVO> getLastTitles() {
		StringBuffer sb = new StringBuffer();
		sb.append("select new ").append(IndexTitleVO.class.getName()).append(IndexTitleVO.STRUCT);
		sb.append(" from ").append(IndexTitle.class.getName()).append(" indextitle,");
		sb.append(UploadBatch.class.getName()).append(" uploadbatch");
		sb.append(" where uploadbatch.status=0 and uploadbatch.batchId=indextitle.batchId");
		sb.append(" and uploadbatch.lastUpdateTime=(select max(lastUpdateTime) from " + UploadBatch.class.getName() + ")");
		Query q = this.getSession().createQuery(sb.toString());
		List<IndexTitleVO> tmpList = (List<IndexTitleVO>) q.list();
		return tmpList;
	}
}
